package com.cool.controller.brandcontroller;

import com.cool.bo.brandbo.BrandBo;
import com.cool.common.CommonResult;
import com.cool.common.ResponseCode;
import com.cool.po.brand.BrandPo;
import com.cool.service.brand.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌管理控制层
 * 黎秦赭
 */
@RestController
public class BrandController {
    @Autowired
    private BrandService brandService;

    @PostMapping("/brand/findAll")
    public CommonResult findAll(@RequestBody BrandBo brandBo) {
        PageHelper.startPage(brandBo.getPageIndex(),brandBo.PAGE_SIZE);
        PageInfo<BrandPo> brandPoPageInfo = new PageInfo<>(brandService.findAll());
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(brandPoPageInfo);
    }

    @PostMapping("/brand/queryByLike")
    public CommonResult queryByLike(@RequestBody BrandBo brandBo) {
        PageHelper.startPage(1,brandBo.PAGE_SIZE);
        PageInfo<BrandPo> brandPoPageInfo = new PageInfo<>(brandService.queryByLike(brandBo.getInputString()));
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(brandPoPageInfo);
    }

    @PostMapping("/brand/insertBrand")
    public CommonResult insertBrand(@RequestBody  BrandBo brandBo) {
        System.out.println(brandBo);
        System.out.println(brandBo.getBrandPo());
        Integer integer = brandService.insertBrand(brandBo.getBrandPo());
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(integer);
    }

    @PostMapping("/brand/updateBrand")
    public CommonResult updateBrand(@RequestBody  BrandBo brandBo) {
        System.out.println(brandBo);
        BrandPo brandPo = brandBo.getBrandPo();
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(brandService.updateBrand(brandPo));
    }


    @PostMapping("/brand/deletBrand")
    public CommonResult deletBrand(@RequestBody  BrandBo brandBo) {
        List<Integer> list = brandBo.getList();
        Integer integer = brandService.deletBrand(list);
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(integer);
    }

}
